﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.DAO;
using System.Data;
using AudioCore.Common;

namespace AudioCore.Biz
{
    public class CategoryBiz : BaseBiz
    {
        public int AddCategory(CategoryEntity category)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    return dao.InsertEntity(category, typeof(CategoryEntity));
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int UpdateCategory(CategoryEntity category)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    return dao.UpdateEntity(category, typeof(CategoryEntity));
                }
            }
            catch (Exception e)
            {

            }
            return -1;
        }

        public int DeleteCategory(int categoryID)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    return dao.DeleteEntity(categoryID);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public List<CategoryEntity> GetAllCategory()
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    DataTable table = dao.GetAllCategory();
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(CategoryEntity)).ConvertAll(item => (CategoryEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryEntity>();
        }

        public List<CategoryEntity> GetCategoryByParentID(int parentID)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    DataTable table = dao.GetCategoryByParentID(parentID);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(CategoryEntity)).ConvertAll(item => (CategoryEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryEntity>();
        }
        
        public bool HasArticle(int categoryID)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    return dao.HasArticle(categoryID);
                }
            }
            catch (Exception e)
            {
                //do
            }
            return false;
        }

        #region Method for service
        public CategoryEntity GetCategoryByID(int categoryID)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    DataTable table = dao.SelectEntityByID(categoryID);
                    if (table.Rows.Count > 0)
                    {
                        return (CategoryEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(CategoryEntity));
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return null;
        }

        public CategoryEntity GetRootCategory()
        {
            return GetCategoryByID(Constant.ROOT_CATEGORY_ID);
        }

        public List<CategoryEntity> GetCategory(int parentID)
        {
            try
            {
                using (CategoryDao dao = new CategoryDao())
                {
                    DataTable table = dao.GetCategory(parentID);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(CategoryEntity)).ConvertAll(item => (CategoryEntity)item);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<CategoryEntity>();
        }

        #endregion

    }
}
